package com.yunke.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunke.common.result.YunkeResult;
import com.yunke.dto.vip.YunkeVipBuyDto;
import com.yunke.service.YunkeVipService;
import com.yunke.vo.vip.YunkeVipLevelVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * VIP会员控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/vip")
@Tag(name = "VIP会员管理", description = "VIP会员相关功能接口")
public class YunkeVipController {

    @Autowired
    private YunkeVipService vipService;

    @GetMapping("/level/list")
    @Operation(summary = "获取VIP等级列表", description = "获取所有可购买的VIP等级信息")
    public YunkeResult<List<YunkeVipLevelVo>> getVipLevelList() {
        try {
            List<YunkeVipLevelVo> result = vipService.getVipLevelList();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取VIP等级列表失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/level/{levelId}")
    @Operation(summary = "获取VIP等级详情", description = "根据等级ID获取VIP等级详细信息")
    public YunkeResult<YunkeVipLevelVo> getVipLevelDetail(
            @Parameter(description = "等级ID") @PathVariable Long levelId) {
        try {
            YunkeVipLevelVo result = vipService.getVipLevelDetail(levelId);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取VIP等级详情失败，等级ID：{}", levelId, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/buy")
    @Operation(summary = "购买VIP会员", description = "用户购买VIP会员")
    public YunkeResult<Map<String, Object>> buyVip(
            @Parameter(description = "VIP购买信息") @Validated @RequestBody YunkeVipBuyDto vipBuyDto) {
        try {
            Map<String, Object> result = vipService.buyVip(vipBuyDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("购买VIP失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/order/create")
    @Operation(summary = "创建VIP订单", description = "创建VIP购买订单")
    public YunkeResult<Map<String, Object>> createVipOrder(
            @Parameter(description = "VIP购买信息") @Validated @RequestBody YunkeVipBuyDto vipBuyDto) {
        try {
            Map<String, Object> result = vipService.createVipOrder(vipBuyDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("创建VIP订单失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/order/status/{orderNo}")
    @Operation(summary = "查询订单状态", description = "根据订单号查询VIP订单状态")
    public YunkeResult<Map<String, Object>> getVipOrderStatus(
            @Parameter(description = "订单号") @PathVariable String orderNo) {
        try {
            Map<String, Object> result = vipService.getVipOrderStatus(orderNo);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("查询VIP订单状态失败，订单号：{}", orderNo, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/my/info")
    @Operation(summary = "获取我的VIP信息", description = "获取当前用户的VIP会员信息")
    public YunkeResult<Map<String, Object>> getMyVipInfo() {
        try {
            Map<String, Object> result = vipService.getMyVipInfo();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取我的VIP信息失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/exchange")
    @Operation(summary = "卡密兑换VIP", description = "使用卡密兑换VIP会员")
    public YunkeResult<Map<String, Object>> exchangeVipCard(
            @Parameter(description = "卡密代码") @RequestParam String camiCode) {
        try {
            Map<String, Object> result = vipService.exchangeVipCard(camiCode);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("卡密兑换VIP失败，卡密：{}", camiCode, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/order/list")
    @Operation(summary = "获取VIP购买记录", description = "获取当前用户的VIP购买历史记录")
    public YunkeResult<Page<Map<String, Object>>> getVipOrderList(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long size) {
        try {
            Page<Map<String, Object>> result = vipService.getVipOrderList(current, size);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取VIP购买记录失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/rights/check")
    @Operation(summary = "检查VIP权益", description = "检查当前用户是否拥有指定VIP权益")
    public YunkeResult<Map<String, Object>> checkVipRights(
            @Parameter(description = "权益名称") @RequestParam String privilege) {
        try {
            Map<String, Object> result = vipService.checkVipRights(privilege);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("检查VIP权益失败，权益：{}", privilege, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/renew")
    @Operation(summary = "VIP续费", description = "续费VIP会员")
    public YunkeResult<Map<String, Object>> renewVip(
            @Parameter(description = "续费信息") @Validated @RequestBody YunkeVipBuyDto renewDto) {
        try {
            Map<String, Object> result = vipService.renewVip(renewDto);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("VIP续费失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @PostMapping("/refund")
    @Operation(summary = "申请VIP退款", description = "申请VIP订单退款")
    public YunkeResult<Map<String, Object>> refundVip(
            @Parameter(description = "订单号") @RequestParam String orderNo,
            @Parameter(description = "退款原因") @RequestParam String reason) {
        try {
            Map<String, Object> result = vipService.refundVip(orderNo, reason);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("申请VIP退款失败，订单号：{}", orderNo, e);
            return YunkeResult.error(e.getMessage());
        }
    }

    // 管理员接口
    @PostMapping("/admin/card/generate")
    @Operation(summary = "生成VIP卡密", description = "管理员生成VIP卡密")
    public YunkeResult<Map<String, Object>> generateVipCard(
            @Parameter(description = "等级ID") @RequestParam Long levelId,
            @Parameter(description = "生成数量") @RequestParam Integer count,
            @Parameter(description = "有效天数") @RequestParam Integer validDays) {
        try {
            Map<String, Object> result = vipService.generateVipCard(levelId, count, validDays);
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("生成VIP卡密失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }

    @GetMapping("/admin/statistics")
    @Operation(summary = "获取VIP统计信息", description = "管理员获取VIP相关统计数据")
    public YunkeResult<Map<String, Object>> getVipStatistics() {
        try {
            Map<String, Object> result = vipService.getVipStatistics();
            return YunkeResult.success(result);
        } catch (Exception e) {
            log.error("获取VIP统计信息失败", e);
            return YunkeResult.error(e.getMessage());
        }
    }
} 